import {Component} from '@angular/core';
import {NavController, NavParams} from 'ionic-angular';
import {StoragePage} from "../storage/storage";
import {BarcodeScanner} from "@ionic-native/barcode-scanner";
import {services} from "../../../providers/service";
import {StorageService} from "../../../providers/storage-service";

@Component({
    selector: 'cjStatus',
    templateUrl: 'cjStatus.html',
})
export class CjStatusPage {
    showAlert = true;
    status = true;
    data;       //数据
    thisData;
    isSuccess: boolean = true;  //默认采集成功状态

    constructor(public navCtrl: NavController,
                public navParams: NavParams,
                private service: services,
                public barcodeScanner: BarcodeScanner,
                private storageServ: StorageService,) {
        this.data = navParams.get("data");
        this.thisData = navParams.get("thisData");
        if (this.data.resultStatus == "success") {
            this.isSuccess = true;      //采集成功
        } else {
            this.isSuccess = false;     //采集失败
        }
        if (this.data.infoData == null) {
            this.data.infoData = {
                boxNo: '',
                outId: '',
                outNo: ''
            }
        }
    }

    toShowAlert() {
        this.showAlert = false;
    }

    btnCancel() {
        this.showAlert = true;
    }

    btnChoose() {
        this.showAlert = true;
        this.navCtrl.popToRoot(StoragePage);
    }

    goback() {
        this.navCtrl.popToRoot(StoragePage);
    }

    //继续抽检
    Tcheck(index) {
        console.log(index);
        this.barcodeScanner.scan().then((barcodeData) => {
            if (barcodeData.text == null || barcodeData.text == '') {
                console.log('取消了扫码');
                this.service.isReturn = true;
                return;
            }
            this.service.boxNo = barcodeData.text;
            let param = {
                "boxNo": barcodeData.text, "outNo": this.thisData.outNo
            }
            this.storageServ.samplingscan(param)
                .then(res => {
                    console.info("收货记录_抽检扫码成功：", res);
                    this.navCtrl.push(CjStatusPage, {data: res, thisData: this.thisData});
                })
                .catch(err => {
                    console.error("收货记录_抽检扫码失败：", err);
                    this.navCtrl.push(CjStatusPage, {data: err, thisData: this.thisData});
                });

        }, (err) => {
            // An error occurred
            console.error(err);
        });

    }

}
